package com.schoolCommunity.controller;


import com.schoolCommunity.entity.Commodity;
import com.schoolCommunity.entity.Information;
import com.schoolCommunity.service.CommodityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author YTH
 * @since 2020-10-10
 */
@CrossOrigin
@RestController
public class CommodityController {

    @Autowired
    private CommodityService commodityService;

    //用户添加一个商品
    @PostMapping("/InsertCommodity")
    public Information InsertCommodity(@RequestBody Commodity commodity) throws IOException {
        return commodityService.InsertCommodity(commodity);
    }

    //添加一张商品的照片
    @PostMapping("/UpdateCommodityPicture/{CommodityId}")
    public Information UpdateCommodityPicture(@RequestParam("file") MultipartFile file, @PathVariable("CommodityId") long CommodityId) throws IOException {
        return commodityService.UpdateCommodityPicture(file, CommodityId);
    }

    //用户主动下架商品
    @GetMapping("/soldOutCommodity/{commodityId}")
    public Information soldOutCommodity(@PathVariable("commodityId") long commodityId) throws IOException {
        return commodityService.soldOutCommodity(commodityId);
    }

    //获取某个用户的所有商品
    @GetMapping("/GetMyCommodity/{memberId}")
    public Information GetMyCommodity(@PathVariable("memberId") long memberId) {
        return commodityService.GetMyCommodity(memberId);
    }

    //分页获取所有商品列表，默认20页
    @GetMapping("/GetCommodityPage/{pageNo}")
    public Information GetCommodityPage(@PathVariable("pageNo") int PageNo) {
        return commodityService.GetCommodityPage(PageNo);
    }

    //获取最大分页
    @GetMapping("/GetMaxPageNo")
    public long GetMaxPageNo() {
        return commodityService.GetMaxPageNo();
    }

    //用户购买商品
    @PostMapping("/BuyCommodity/{BuyMemberId}/{CommodityId}")
    public Information BuyCommodity(@PathVariable("BuyMemberId") long BuyMemberId, @PathVariable("CommodityId") long CommodityId) {
        return commodityService.BuyCommodity(BuyMemberId, CommodityId);
    }

    //管理员功能，删除某个产品
    @GetMapping("/DeleteCommodity/{commodityId}")
    public Information DeleteCommodity(@PathVariable("commodityId") long commodityId) {
        return commodityService.DeleteCommodity(commodityId);
    }

    //管理员功能，审核某个产品
    @GetMapping("/SuggestCommodity/{commodityId}/{suggestResult}")
    public Information SuggestCommodity(@PathVariable("commodityId") long commodityId, @PathVariable("suggestResult") int suggestResult) {
        return commodityService.SuggestCommodity(commodityId, suggestResult);
    }

    //管理员功能，分页获取尚未审核的商品
    @GetMapping("/GetNoSuggestCommodity/{pageNo}")
    public Information GetNoSuggestCommodity(@PathVariable("pageNo") long pageNo) {
        return commodityService.GetNoSuggestCommodity(pageNo);
    }

    //管理员功能，获取最大为分页商品页数
    @GetMapping("/GetNoSuggestCommodityMaxPage")
    public long GetNoSuggestCommodityMaxPage() {
        return commodityService.GetNoSuggestCommodityMaxPage();
    }

    //管理员功能，获取所有未上架的商品，分页
    @GetMapping("/GetAllNoOnCommodity/{pageNo}")
    public Information GetAllNoOnCommodity(@PathVariable("pageNo") long pageNo){
        return commodityService.GetAllNoOnCommodity(pageNo);
    }

    //最大下架页数
    @GetMapping("/GetAllNoOnCommodityMaxPage")
    public  long GetAllNoOnCommodityMaxPage(){
        return commodityService.GetAllNoOnCommodityMaxPage();
    }

    //管理员功能？上架某个商品
    @GetMapping("/PutOnCommodity/{CommodityId}")
    public Information PutOnCommodity(@PathVariable("CommodityId") long commodityId){
        return commodityService.PutOnCommodity(commodityId);
    }



}

